package com.example.navigation.api

import android.util.Log
import dagger.Module
import dagger.Provides
import dagger.hilt.InstallIn
import dagger.hilt.components.SingletonComponent
import okhttp3.Interceptor
import okhttp3.Response
import javax.inject.Singleton

/**
 * Created with IntelliJ IDEA.
 * @Author: Liao Run
 * @Date: 2024-06-08 15:28
 * @Description:
 **/
@Module
@InstallIn(SingletonComponent::class)
class TokenInterceptor : Interceptor {
    var token: String = ""
    override fun intercept(chain: Interceptor.Chain): Response {
        val originalRequest = chain.request()
        //key的话以后台给的为准，我这边是叫token
        val updateRequest = originalRequest.newBuilder().header("token", token).build()
        Log.e(this.toString(), "intercept: 成功添加token:${token}")
        val response = chain.proceed(updateRequest)
        return response
    }

    @Provides
    @Singleton
    fun getTokenInterceptor(): TokenInterceptor {
        return TokenInterceptor()
    }
}